import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class test4 {
    private JTextField playlistNameField;
    private JButton createPlaylistButton;
    private JPanel jp;
    private JButton backbtn;
    private JButton listbtn;
    public JTable table1;
    private String currentUsername; // 当前登录的用户名
    JFrame jf = new JFrame("音乐播放器 - 创建歌单");
    public test4(String username) {
        this.currentUsername = username;
        jf.setSize(500, 500);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        createPlaylistButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createPlaylist();

            }
        });
        listbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showPlaylists();

            }
        });
        table1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    int selectedRow = table1.getSelectedRow();
                    if (selectedRow >= 0) {
                        String selectedPlaylistName = (String) table1.getValueAt(selectedRow, 0);
                        new test5(currentUsername,selectedPlaylistName);
                        jf.setVisible(false);
                    }
                }
            }
        });
        backbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new test3(currentUsername);
                jf.setVisible(false);
            }
        });

        jf.add(jp);
        jf.setVisible(true);

    }

    private void createPlaylist() {
        String playlistname = playlistNameField.getText();
        if (!playlistname.isEmpty()) {
            // 连接数据库并创建歌单
            String sql = "INSERT INTO playlists (playlistname, userid) VALUES (?, (SELECT userid FROM users WHERE username = ?))";
            try (Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mysql", "root", "190825DLdl");
                 PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, playlistname);
                pstmt.setString(2, currentUsername);
                int rowsAffected = pstmt.executeUpdate();
                if (rowsAffected > 0) {
                    JOptionPane.showMessageDialog(jf, "创建成功", "创建信息", JOptionPane.INFORMATION_MESSAGE);
                    playlistNameField.setText("");
                } else {
                    JOptionPane.showMessageDialog(jf, "创建失败", "错误信息", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(jf, "数据库错误。" + ex.getMessage());
            }
        } else {
            JOptionPane.showMessageDialog(jf, "请输入歌单名称。");
        }
    }
    private void showPlaylists() {
        String sql = "SELECT playlistname FROM playlists WHERE userid = (SELECT userid FROM users WHERE username = ?) ";
        try (Connection conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/mysql", "root", "190825DLdl"); // 替换为你的数据库名称和密码
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, currentUsername);
            ResultSet resultSet = pstmt.executeQuery();
            String[] columnNames = {"《《歌单列表》》"};
            DefaultTableModel model = new DefaultTableModel(columnNames, 0);
            while (resultSet.next()) {
                String playlistName = resultSet.getString("playlistname");
                model.addRow(new Object[]{playlistName});
            }
            table1.setModel(model);
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(jf, "数据库错误：" + ex.getMessage());
        }
    }
//
//    public static void main(String[] args) {
//        new test4();
//    }
}